package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.DistrictService;
import com.bst.bsj.common.dal.dataobject.Area;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/district")
public class DistrictController extends WebBaseController {

  @Autowired
  private DistrictService districtService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月14日 下午3:41:51
  * @modificator :tonglu
  * @modify date :2016年5月14日 下午3:41:51
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/districtList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("area/district");
    String name = (String) params.get("name");
    String city = (String) params.get("city");
    String province = (String) params.get("province");
    String status = (String) params.get("status");
    if (city != null) {
      city = city.trim();
    }
    if (name != null) {
      name = name.trim();
    }
    if (province != null) {
      province = province.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(status)) {
      paramsMap.put("status", status);
    }
    if (StringUtil.isNotBlank(name)) {
      paramsMap.put("name", name);
    }
    if (StringUtil.isNotBlank(city)) {
      paramsMap.put("city", city);
    }
    if (StringUtil.isNotBlank(province)) {
      paramsMap.put("province", province);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = districtService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<Area> areas = (List<Area>) result.getModels().get("areas");
      modelAndView.addObject("areas", areas);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    return modelAndView;
  }

  @RequestMapping(value = "/changeStatus")
  public void changeStatus(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    String status = (String) params.get("status");//状态
    Area area = new Area();
    area.setId(Long.valueOf(id));
    if (status != null) {
      area.setStatus(status);
    }
    districtService.update(area);
  }

  /**
   * 
  * @Title: edit
  * @Description: TODO(编辑)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午7:34:50
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午7:34:50
   */
  @RequestMapping(value = "/edit")
  public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    Area area = districtService.getArea(Long.valueOf(id));
    modelAndView.addObject("area", area);
    return modelAndView;
  }

  /**
   * 
  * @Title: getAreas
  * @Description: TODO(根据类型查询area)
  * @param req
  * @param resp
  * @param type
  * @return
  * @throws Exception
  * List<Area>
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月16日 下午1:36:05
  * @modificator :tonglu
  * @modify date :2016年5月16日 下午1:36:05
   */
  @RequestMapping(value = "/getAreas")
  @ResponseBody
  public List<Area> getAreas(HttpServletRequest req, HttpServletResponse resp, String type) throws Exception {
    List<Area> list = districtService.getAreas(type);
    return list;
  }

  /**
   * 
  * @Title: getAreasByParent
  * @Description: TODO(根据parent查询)
  * @param req
  * @param resp
  * @param parent
  * @return
  * @throws Exception
  * List<Area>
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月16日 下午5:01:22
  * @modificator :tonglu
  * @modify date :2016年5月16日 下午5:01:22
   */
  @RequestMapping(value = "/getAreasByParent")
  @ResponseBody
  public List<Area> getAreasByParent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String,Object> paramMap=new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);//参数
    String parentStr=(String)params.get("parent");
    String status=(String)params.get("status");
    if(StringUtil.isNotEmpty(parentStr)){
      paramMap.put("parent", Long.parseLong(parentStr));
    }
    if(StringUtil.isNotEmpty(status)){
      paramMap.put("status", status);
    }
    
    
    List<Area> list = districtService.getAreasByParent(paramMap);
    return list;
  }

  @RequestMapping(value = "/save")
  @ResponseBody
  public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramMap = new HashMap<String, Object>();
    Area area = new Area();
    String province = (String) params.get("province");
    String city = (String) params.get("city");
    String name = (String) params.get("name");
    String id = (String) params.get("id");
    if (name != null) {
      name = name.trim();
    }
    if (StringUtil.isNotBlank(name)) {
      paramMap.put("name", name);
    }
    paramMap.put("province", province);
    paramMap.put("name", name);
    Area areas = districtService.getAreaByCity(paramMap);
    if (id != null) {
      area = districtService.getArea(Long.valueOf(id));
      if(areas==null){
        area.setName(name);
        area.setParent(Long.valueOf(city));
        Area provinceArea = districtService.getArea(Long.valueOf(province));
        area.setProvince(provinceArea.getProvince());
        Area cityArea = districtService.getArea(Long.valueOf(city));
        area.setCity(cityArea.getCity());
        districtService.update(area);
        return "success";
      }else{
        if (!id.equals(areas.getId().toString())) {
          return "fail";
        } else {
          area.setName(name);
          area.setParent(Long.valueOf(city));
          Area provinceArea = districtService.getArea(Long.valueOf(province));
          area.setProvince(provinceArea.getProvince());
          Area cityArea = districtService.getArea(Long.valueOf(city));
          area.setCity(cityArea.getCity());
          districtService.update(area);
          return "success";
        }
      }
    } else {
      if (areas != null) {
        return "fail";
      } else {
        area.setName(name);
        area.setType("district");
        area.setParent(Long.valueOf(city));
        Area provinceArea = districtService.getArea(Long.valueOf(province));
        area.setProvince(provinceArea.getProvince());
        Area cityArea = districtService.getArea(Long.valueOf(city));
        area.setCity(cityArea.getCity());
        area.setStatus("U");
        List<Area> list = districtService.getAreas("district");
        area.setDistrict(Long.valueOf(list.size() + 1));
        districtService.insert(area);
        return "success";
      }
    }
  }

  @RequestMapping(value = "/delete")
  public void delete(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    districtService.delete(Long.valueOf(id));
  }
}
